package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpServletRequest request;


    /**
     *  后台管理登录
     * <p>
     * <p>
     * 1.接受用户名和密码
     * 2.判断用户名是否存在
     * 3.判断用户密码
     * 3.1将用户密码加密处理
     * 4.判断用户状态
     * 5.给前端返回数据
     * 6.存进session
     *
     * @param employee
     * @param
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee) {
        // 1将接受到的密码加密处理
        String password = employee.getPassword();
        String passwordForm = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.判断用户名是否存在
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, username);
        Employee emp = employeeService.getOne(lqw);
        if (emp == null) {
            return R.error("登录失败");
        }

        //3.判断用户密码
        if (!passwordForm.equals(emp.getPassword())) {
            return R.error("登录失败");
        }

        //4.判断用户状态
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        //5.存进session
        HttpSession session = request.getSession();
        session.setAttribute("employee", emp.getId());

        //6.给前端返回数据
        return R.success(emp);
    }

    /**
     * 后台退出
     *
     * @return
     */
    @PostMapping("/logout")
    public R logout() {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * TODO 新增
     * 由于第一次添加没有密码需要设置一个密码  密码为 123465并且md5加密
     * 2.调用方法添加
     *
     * @param employee
     * @return
     */

    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        log.info("新增员工");
        //由于第一次添加没有密码需要设置一个密码  密码为 123465并且md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2. 调用方法添加
        boolean save = employeeService.save(employee);
        if (save) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    /**
     *  分页
     * 配置分页的插件
     * 创建分页构造器
     * 添加过滤条件
     * 添加排序条件
     * 执行
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name) {
        log.info("分页page={},pageSize={},name={}", page, pageSize, name);
        //配置分页的插件
        Page<Employee> employeePage = new Page<>(page, pageSize);
        //创建分页构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        //执行
        employeeService.page(employeePage, lqw);
        if (employeePage != null) {
            System.out.println(11);
        }
        return R.success(employeePage);
    }


    /**
     * 修改状态
     * 1.获取当前登录信息填充修改人
     * 2.填充修改时间
     * 3.修改
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        log.info("修改状态:{}", employee.toString());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     *  根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long  id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
          return R.success(employee);
      }
      return R.error("没有查询到信息");
    }
}
